package com.yangchoi.jetpackcompose.route

import android.net.Uri
import android.os.Parcelable
import androidx.annotation.StringRes
import androidx.navigation.NavHostController
import com.yangchoi.jetpackcompose.R
import com.yangchoi.jetpackcompose.utils.toJson


enum class RouteKey{
    HOME, // 首页
    PROJECT,//项目页
    NAVI,//导航页
    MINE,//个人中心页
    WEB_VIEW,
    LOGIN
}

object RouteNavigationUtil{

    fun navigationTo(
        navCtrl: NavHostController,
        destinationName: String,
        args: Any? = null,
        backStackRouteName: String? = null,
        isLaunchSingleTop: Boolean = true,
        needToRestoreState: Boolean = true,
    ) {

        var singleArgument = ""
        if (args != null) {
            when (args) {
                is Parcelable -> {
                    singleArgument = String.format("/%s", Uri.encode(args.toJson()))
                }
                is String -> {
                    singleArgument = String.format("/%s", args)
                }
                is Int -> {
                    singleArgument = String.format("/%s", args)
                }
                is Float -> {
                    singleArgument = String.format("/%s", args)
                }
                is Double -> {
                    singleArgument = String.format("/%s", args)
                }
                is Boolean -> {
                    singleArgument = String.format("/%s", args)
                }
                is Long -> {
                    singleArgument = String.format("/%s", args)
                }
            }
        }
        navCtrl.navigate("$destinationName$singleArgument") {
            if (backStackRouteName != null) {
                popUpTo(backStackRouteName) { saveState = true }
            }
            launchSingleTop = isLaunchSingleTop
            restoreState = needToRestoreState
        }
    }

    fun NavHostController.back() {
        navigateUp()
    }
}

/**
 * @param key 路由key
 * @param stringId 导航栏显示的字符串
 * @param iconUnSelected 未选中显示的图标
 * @param iconSelected 选中显示的图标
 */
sealed class BottomTabBarRoute(
    var key: RouteKey, @StringRes var stringId:Int,
    var iconUnSelected: Int,
    var iconSelected: Int){

    object Home: BottomTabBarRoute(RouteKey.HOME, R.string.bottom_bar_home, R.drawable.icon_home_unselect, R.drawable.icon_home_selected)
    object Project: BottomTabBarRoute(RouteKey.PROJECT, R.string.bottom_bar_project, R.drawable.icon_project_unselect, R.drawable.icon_project_selected)
    object Navi: BottomTabBarRoute(RouteKey.NAVI, R.string.bottom_bar_navi, R.drawable.icon_navi_unselected, R.drawable.icon_navi_selected)
    object Mine: BottomTabBarRoute(RouteKey.MINE, R.string.bottom_bar_mine, R.drawable.icon_mine_unselect, R.drawable.icon_mine_selected)

}
